package requests

import (
	"github.com/thedevsaddam/govalidator"
	"goblog/app/models/user"
)

// ValidateResetPasswordForm 重置密码
func ValidateResetPasswordForm(data user.User) map[string][]string {
	// 1. 表单规则
	rules := govalidator.MapData{
		"password":         []string{"required", "min:6"},
		"password_confirm": []string{"required", "min:6"},
	}

	// 2 定制错误消息
	messages := govalidator.MapData{
		"password": []string{
			"required:密码 为必填项",
			"min:密码 长度需大于6",
		},
		"password_confirm": []string{
			"required:确认密码 为必填项",
			"min:确认密码 长度需大于6",
		},
	}

	// 3. 配置选项
	ops := govalidator.Options{
		Data:          &data,
		Rules:         rules,
		TagIdentifier: "valid", // Struct 标签标识符
		Messages:      messages,
	}

	// 4. 开始验证
	errs := govalidator.New(ops).ValidateStruct()

	// 5. 因 govalidator 不支持 password_confirm 验证
	if data.Password != data.PasswordConfirm {
		errs["password_confirm"] = append(errs["password_confirm"], "两次输入的密码不一致")
	}

	return errs
}
